/**
 * @file ui_honeycomb.c
 * @author your name (you@domain.com)
 * @brief 
 * @version 0.1
 * @date 2024-04-06
 * 
 * @copyright Copyright (c) 2024
 * 
 */

#include "ui_honeycomb.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define IMAGE_ROW_NUM   7
#define IMAGE_SCALE     0.4f
#define IMAGE_SIZE      100

ui_honeycomb_t *ui_honeycomb_create(lv_obj_t *parent)
{
    ui_honeycomb_t *honeycomb = malloc(sizeof(ui_honeycomb_t));
    if(honeycomb == NULL)
    {
        return NULL;
    }
    memset(honeycomb, 0, sizeof(ui_honeycomb_t));
  
    lv_obj_set_scrollbar_mode(parent,LV_SCROLLBAR_MODE_OFF);
    lv_obj_set_scroll_dir(parent,LV_DIR_NONE);

    honeycomb->parent = parent;

    return honeycomb;
}

static void img_drag_event_handler(lv_event_t * e)
{
    int i;
    lv_sqrt_res_t lenth;
    lv_sqrt_res_t lenth_max;
    ui_honeycomb_t *honeycomb = (ui_honeycomb_t *)lv_event_get_user_data(e);
    int w = lv_obj_get_width(honeycomb->parent);
    int h = lv_obj_get_height(honeycomb->parent);
    lv_obj_t * obj = lv_event_get_target(e);
    lv_indev_t * indev = lv_indev_get_act();
    if(indev == NULL)  return;

    lv_point_t vect;
    lv_indev_get_vect(indev, &vect);

    honeycomb->point_final.x += vect.x;
    honeycomb->point_final.y += vect.y;

    for(i = 0;i < honeycomb->cur_index;i++)
    {
        int pos_x,pos_y;
        pos_x = honeycomb->init_pos[i].x + honeycomb->point_final.x;
        pos_y = honeycomb->init_pos[i].y + honeycomb->point_final.y;
        //计算图片距离屏幕中心距
        lv_sqrt((pos_x - w / 2) * (pos_x - w / 2) + \
                (pos_y - h / 2) * (pos_y - h / 2), \
                &lenth, 0xFFFF);
        lv_sqrt((w / 2) * (w / 2) + (h / 2) * (h / 2),&lenth_max, 0xFFFF); 
        float zoom = 1.0 - (float)(lenth.i) / (float)lenth_max.i; 

        lv_img_set_zoom(honeycomb->image[i], zoom * 256);
     ; 
        //lv_obj_set_style_transform_zoom(honeycomb->image[i],(400 - lenth.i) * IMAGE_SCALE,_LV_STYLE_STATE_CMP_SAME);
        lv_obj_set_pos(honeycomb->image[i], pos_x - (400 - lenth.i) * 0.1, pos_y - (400 - lenth.i) * 0.1);
    }
}

bool ui_honeycomb_add(ui_honeycomb_t *honeycomb, lv_obj_t *img_item)
{
     int x,y,l,w,h,i;
     lv_sqrt_res_t lenth;
     lv_sqrt_res_t lenth_max;
     w = lv_obj_get_width(honeycomb->parent);
     h = lv_obj_get_height(honeycomb->parent);
 
     i = honeycomb->cur_index;

     if(i >= UI_HONEYCOMB_MAX_ITEM_NUM)
     {
        return false;
     }

     honeycomb->image[honeycomb->cur_index++] = img_item;

     if((i / IMAGE_ROW_NUM) % 2){
            x = (i % IMAGE_ROW_NUM) * IMAGE_SIZE + IMAGE_SIZE / 2;
        }
        else {
            x = (i % IMAGE_ROW_NUM) * IMAGE_SIZE;
        }

     y = (i / IMAGE_ROW_NUM) * IMAGE_SIZE * 0.886f;

    lv_sqrt((x - w / 2) * (x - w / 2) + (y - h / 2) * (y - h / 2), &lenth, 0xFFFF);
    
    lv_sqrt((w / 2) * (w / 2)+(h / 2) * (h / 2),&lenth_max, 0xFFFF); 
    float zoom = 1.0 - (float)(lenth.i) / (float)lenth_max.i; 
     lv_img_set_zoom(honeycomb->image[i], zoom * 256);

    //lv_obj_set_style_transform_zoom(honeycomb->image[i],(w / 2 - len.i) * IMAGE_SCALE,_LV_STYLE_STATE_CMP_SAME);

    lv_obj_set_x(honeycomb->image[i], x);
    lv_obj_set_y(honeycomb->image[i], y);
    honeycomb->init_pos[i].x = x;
    honeycomb->init_pos[i].y = y;
    lv_obj_add_flag(honeycomb->image[i],LV_OBJ_FLAG_IGNORE_LAYOUT);
    lv_obj_add_flag(honeycomb->image[i],LV_OBJ_FLAG_CLICKABLE);
    lv_obj_add_event_cb(honeycomb->image[i],img_drag_event_handler, LV_EVENT_PRESSING, honeycomb);

    return true;
}